<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Derived from CakePHP's TimeHelper
 * Copyright (c) 2006, Cake Software Foundation, Inc.
 * @link http://cakephp.org/
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package    FeatherCMS
 * @subpackage Libs
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://feather.portalier.com
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class Timer
{
	static function fromString($date_string)
	{
		return is_integer($date_string) ? $date_string : strtotime($date_string);
	}
	
	static function date($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		return strftime(_("%Y-%m-%d"), $date);
	}
	
	static function niceDate($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		return ucfirst(strftime(_("%B %e, %Y"), $date));
	}
	
	static function nice($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		return strftime(_("%A %B %e %Y, %I:%M%p"), $date);
	}
	
	static function niceShort($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		
		if (self::isToday($date))
			return s_('today, %s', date("H:i", $date));
		elseif (self::wasYesterday($date))
			return s_('yesterday, %s', date( "H:i", $date));
		else
			return strftime(self::isThisYear($date) ? _("%B %e, %I:%M%p") : _("%B %e %Y, %I:%M%p"), $date);
	}
	
	static function short($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string): time();
		$ret = strftime(self::isThisYear($date) ? _("%b %e, %I:%M%p") : _("%b %e %Y, %I:%M%p"), $date);
		return $ret;
	}
	
	static function isInFuture($string)
	{
		return (self::fromString($string) > time());
	}
	
	static function isToday($date_string)
	{
		return date('Y-m-d', self::fromString($date_string)) == date( 'Y-m-d', time());
	}	
	
	static function isThisYear($date_string)
	{
		return date('Y', self::fromString($date_string)) == date( 'Y', time());
	}
	
	static function wasYesterday($date_string)
	{
		return date('Y-m-d', self::fromString($date_string)) == date( 'Y-m-d', strtotime('yesterday'));
	}
	
	static function isTomorrow($date_string)
	{
		return date('Y-m-d', self::fromString($date_string)) == date('Y-m-d', strtotime('tomorrow'));
	}
	
	static function toAtom($date_string)
	{
		return date('Y-m-d\TH:i:s\Z', self::fromString($date_string));
	}
	
	static function toRSS($date_string)
	{
		return date('r', self::fromString($date_string));
	}
	
	static function SQL($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		return date('Y-m-d H:i:s', self::fromString($date));
	}
}
?>